package com.liang.circle.server.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.liang.auth.api.entity.Result;
import com.liang.circle.api.common.PageDTO;
import com.liang.circle.api.req.ShareMomentGetReq;
import com.liang.circle.api.req.ShareMomentRemoveReq;
import com.liang.circle.api.req.ShareMomentSaveReq;
import com.liang.circle.api.vo.ShareMomentVO;
import com.liang.circle.server.sensitive.WordFilter;
import com.liang.circle.server.service.ShareMomentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/circle/share/moment")
@RequiredArgsConstructor
public class ShareMomentController {

    private final WordFilter wordFilter;
    private final ShareMomentService shareMomentService;

    /**
     * 发布内容
     */
    @PostMapping("/save")
    public Result<Void> save(@RequestBody ShareMomentSaveReq requestParam) {
        try {
            log.info("发布动态入参：{}", JSON.toJSONString(requestParam));

            Preconditions.checkNotNull(requestParam, "参数不能为空");
            Preconditions.checkNotNull(requestParam.getCircleId(), "圈子id不能为空");
            Preconditions.checkNotNull(requestParam.getContent(), "内容不能为空");

            wordFilter.check(requestParam.getContent());
            shareMomentService.saveMoment(requestParam);
            return Result.ok("发布内容成功");
        } catch (Exception e) {
            log.error("发布动态异常！错误原因{}", e.getMessage(), e);
            return Result.fail("发布动态异常！" + e.getMessage());
        }
    }

    /**
     * 分页查询鸡圈内容
     */
    @PostMapping("/getMoments")
    public Result<PageDTO<ShareMomentVO>> getMomentList(@RequestBody ShareMomentGetReq requestParam) {
        try {
            log.info("查询动态入参：{}", JSON.toJSONString(requestParam));

            PageDTO<ShareMomentVO> list = shareMomentService.getMomentList(requestParam);
            return Result.ok(list);
        } catch (Exception e) {
            return Result.fail("查询动态异常！");
        }
    }

    /**
     * 删除圈子内容
     */
    @PostMapping("/remove")
    public Result<Void> remove(@RequestBody ShareMomentRemoveReq requestParam) {
        try {
            log.info("删除圈子内容入参：{}", JSON.toJSONString(requestParam));

            Preconditions.checkNotNull(requestParam, "参数不能为空");
            Preconditions.checkNotNull(requestParam.getId(), "动态ID不能为空");

            shareMomentService.removeMoment(requestParam.getId());
            return Result.ok("删除圈子内容成功");
        } catch (Exception e) {
            log.error("删除圈子内容异常！错误原因：{}", e.getMessage(), e);
            return Result.fail("删除圈子内容异常！");
        }
    }
}
